<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <script src="./layui/layui.js"></script>
    <script src="./js/global.js"></script>
</head>
<body>
    <div style="margin-right: 40px; width: 400px;">
        <form class="layui-form" id="addForm">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username" required  lay-verify="required" placeholder="请输入用户名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password" required lay-verify="required" placeholder="请输入密码"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                    <input type="password" name="repassword" required lay-verify="required" placeholder="请输入确认密码"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="text" name="age" required lay-verify="required" placeholder="请输入年龄"
                        class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">头像</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-normal" id="avatar">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <input type="hidden" id="avatarPath" name="avatar">
                    <img src="" id="avatarImg" style="width: 40px; height: 40px; border-radius: 50%; display: none;">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit lay-filter="add" style="width: 180px;">添&nbsp;&nbsp;&nbsp;&nbsp;加</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use(['layer','form', 'upload'],function () {
            var layer = layui.layer
            var $ = layui.jquery
            var form = layui.form
            var upload = layui.upload;
   
            //执行实例
            var uploadInst = upload.render({
                elem: '#avatar', //绑定元素
                url: BASE_URL + '/upload', //上传接口
                done: function(res){  //上传完毕回调
                    console.log(res)
                    $('#avatarPath').val(res.data)
                    $('#avatarImg').attr('src', BASE_URL + res.data).show()
                    // 鼠标点击图片显示大图
                    $('#avatarImg').click(function(){
                        layer.open({
                            type: 1,
                            title: false,
                            closeBtn: 0,
                            area: '600px',
                            skin: 'layui-layer-nobg', //没有背景色
                            shadeClose: true,
                            content: '<img src="'+ BASE_URL + res.data +'" style="width: 600px; height: 600px;">'
                        });
                    })
                },
                error: function(err){  //请求异常回调
                   layer.msg('上传失败', {icon: 5})
                }
            });

            form.on('submit(add)',function(data){
                $.ajax({
                    type:'post',
                    url:BASE_URL + '/user/register',
                    data:data.field,
                    dataType:'json',
                    success:function(res){
                        if(res.status == 200){
                            layer.msg('添加成功',{icon:1,time:1000},function(){
                                $('#addForm')[0].reset()
                                // 添加成功后，点击左侧的链接， 跳转到管理员列表页面
                                $('a[href="listuser.html"]',parent.document)[0].click()
                            })
                        }
                    }
                })
            })

        })
    </script>
</body>
</html>